/*
 * @Author: C.
 * @Date: 2023-06-12 10:26:44
 */
import { ref } from 'vue';
/**
 * @description: 控制缓冲时间 ，可以让loading的时长控制在固定的时间上，超出时长则继续loading，直到任务结束
 * @return {*}
 */
export function usePeriod(timeout: number) {
  const loading = ref<boolean>(false);
  const beginTime = ref(0);
  function start() {
    if (loading.value) return;
    loading.value = true;
    beginTime.value = new Date().getTime();
    end();
  }
  function end() {
    const period = beginTime.value + timeout - new Date().getTime();
    if (period <= 0) {
      loading.value = false;
    } else {
      setTimeout(() => {
        loading.value = false;
      }, period);
    }
  }
  return {
    start,
    end,
    loading,
  };
}
